package com.ddogring.business.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddogring.business.domain.Comment;
import com.ddogring.business.domain.Commodity;
import com.ddogring.business.domain.Remarks;
import com.ddogring.business.mapper.CommodityMapper;
import com.ddogring.business.service.ICommentService;
import com.ddogring.business.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: eshop
 * @BelongsPackage: com.ddogring.business.service.impl
 * @Author: DdogRing
 * @CreateTime: 2023-01-30  11:26
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements ICommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private ICommentService commentService;

    /**
     * @description: 查询商品列表
     * @author: DdogRing
     * @date: 2023/1/31 14:44
     * @param: [commodity]
     * @return: java.util.List<com.ddogring.business.domain.Commodity>
     **/
    @Override
    public List<Commodity> selectCommodityList(Commodity commodity) {
        return commodityMapper.selectCommodityList(commodity);
    }

    /**
     * @description: 查询商品详情
     * @author: DdogRing
     * @date: 2023/2/1 16:17
     * @param: [id]
     * @return: com.ddogring.business.domain.Commodity
     **/
    @Override
    public Commodity getCommodityInfo(Integer id) {

        Commodity commodity = commodityMapper.selectById(id);
        List<JSONObject> resultList = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("title", "商品名称");
        jsonObject.put("content", commodity.getCommodity());
        resultList.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("title", "商品编号");
        jsonObject.put("content", commodity.getId());
        resultList.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("title", "商品描述");
        jsonObject.put("content", commodity.getDescription());
        resultList.add(jsonObject);
        jsonObject = new JSONObject();
        jsonObject.put("title", "是否包邮");
        jsonObject.put("content", commodity.getIsMail());
        resultList.add(jsonObject);
        if (1 == commodity.getIsMail() && null != commodity.getMailPrice()) {
            jsonObject = new JSONObject();
            jsonObject.put("title", "包邮价格");
            jsonObject.put("content", commodity.getMailPrice());
            resultList.add(jsonObject);
        }
        jsonObject = new JSONObject();
        jsonObject.put("title", "剩余数量");
        jsonObject.put("content", commodity.getRemainder() + "/" + commodity.getTotal());
        resultList.add(jsonObject);
        commodity.setInfo(resultList);

        List<Comment> commentList = commentService.selectByCommodityId(id);
        Remarks remarks = new Remarks();
        remarks.setDetail(commentList);
        remarks.setGoodAnalyse(null != commentList && commentList.size() > 0 ? commentList.get(0).getGoodAnalyse() : 0);
        List<Integer> numDetail = new ArrayList<>();
        numDetail.add(null != commentList && commentList.size() > 0 ? commentList.get(0).getHigh() : 0);
        numDetail.add(null != commentList && commentList.size() > 0 ? commentList.get(0).getMiddle() : 0);
        numDetail.add(null != commentList && commentList.size() > 0 ? commentList.get(0).getLow() : 0);
        remarks.setRemarksNumDetail(numDetail);
        commodity.setRemarks(remarks);

        return commodity;
    }

    /**
     * @description: 修改商品库存
     * @author: DdogRing
     * @date: 2023/2/8 17:50
     * @param: [shopId]
     * @return: int
     **/
    @Override
    public int updateRemainder(Integer shopId) {
        return commodityMapper.updateRemainder(shopId);
    }

    /**
     * @description: 商品编辑
     * @author: DdogRing
     * @date: 2023/2/9 10:49
     * @param: [commodity]
     * @return: int
     **/
    @Override
    public int editCommodity(Commodity commodity) {
        return commodityMapper.editCommodity(commodity);
    }
}
